//
//  DRFoodTherapyVC.m
//  DoctorRen
//
//  Created by Lemon on 2018/11/19.
//

#import "DRFoodTherapyVC.h"
#import "DRFoodTherapyCell.h"
#import "DRLoginDataModel.h"
#import "DRFoodTherapyModel.h"

@interface DRFoodTherapyVC ()<UITableViewDataSource ,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<DRFoodTherapyModel *> *cellDatas;

@end

@implementation DRFoodTherapyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self reloadData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [self setKefuButtonHidden:NO];
    [self reloadData];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];

}

-(void) reloadData{
    [[DRHttpTool shared] POST:DRURL(@"index.php/NewApiIndex/manage") andParams:@{@"token": [DRLoginDataModel getUserToken]} andSucc:^(id  _Nonnull data) {
        [self.cellDatas removeAllObjects]; 
        [self.cellDatas addObject:[self creatModelWithUrl:data[@"phan_url"] image:@"phan_url" title:@"食疗计划"]];
        [self.cellDatas addObject:[self creatModelWithUrl:data[@"record_url"] image:@"record_url" title:@"饮食记录"]];
        [self.cellDatas addObject:[self creatModelWithUrl:data[@"addplan_url"] image:@"addplan_url" title:@"配餐方案"]];
        [self.cellDatas addObject:[self creatModelWithUrl:data[@"device_url"] image:@"device_url" title:@"设备数据"]];
        [self.cellDatas addObject:[self creatModelWithUrl:data[@"info_url"] image:@"info_url" title:@"健康档案"]];
        [self.cellDatas addObject:[self creatModelWithUrl:data[@"selftest_url"] image:@"selftest_url" title:@"健康自测"]];
        [self.tableView reloadData];
    } andFail:^(NSError * _Nonnull error) {
        if (error.code == 500) {
            [SVProgressHUD dismiss];
            WEAKSELF
            if (![DRLoginDataModel isLogin]) {
                return ;
            }
            [DRLoginDataModel setIsLogin:NO];
            [self presentAlertViewWithTitle:@"请重新登录" message:@"" confirmTitle:@"确定" handler:^{
                [weakSelf gotoLogin];
            }];
        }else{
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
            
        }
    }];
}

-(DRFoodTherapyModel *) creatModelWithUrl:(NSString *)url image:(NSString *)image title:(NSString *)title{
    DRFoodTherapyModel *model = [DRFoodTherapyModel new];
    model.url = url;
    model.image = image;
    model.title = title;
    return model;
}

-(NSMutableArray *)cellDatas{
    if (!_cellDatas) {
        _cellDatas = [NSMutableArray array];
    }
    return _cellDatas;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cellDatas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DRFoodTherapyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DRFoodTherapyCell.h" forIndexPath:indexPath];
    [cell setItme:self.cellDatas[indexPath.row]];
    return  cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    if (indexPath.row == 3) {
        [self pushDevicesVC];
        [self setKefuButtonHidden:YES];
    }else{
        [self pushWebViewWithUrl:self.cellDatas[indexPath.row].url];
    }
}

@end
